package com.companyname.common.util.xml;

import java.io.*;

import java.util.*;

import org.jdom.*;

import org.jdom.output.*;

import org.jdom.input.*;

import org.apache.log4j.Logger;

/**
 * <p>Description: 读取实体配置文件</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: 公司名</p>
 * @ $Author: amon_lei $
 * @ $Date: 2005/03/10 16:04:17 $
 * @ $Revision: 1.4 $ 
 * @ created in 2005-2-15 9:57
 *
 */

public class ConfigReader
{
        static Logger logger = Logger.getLogger(ConfigReader.class);        
        
        private static Map Cache;
                
        private String entityName;
        
        static
        {
                Cache = new HashMap();        
        } 

        public ConfigReader(String entityName)
        {
                this.entityName = entityName;
                
        }
        
        public ConfigBean reading() throws ConfigReadException
        {
                Object value = this.Cache.get(this.entityName);
                if(value == null)
                {
                        ConfigBean bean = this.parse();
                        this.addCache(bean);
                        return bean;
                }
                else
                {
                        return ((ConfigBean)value).getClone();
                }
                
        }
        
        public String getEntityName()
        {
                return this.entityName;        
        }
        
        private void addCache(Object value)
        {
                logger.debug("addCacne()");
                synchronized(this.Cache)       
                {
                        this.Cache.put(this.entityName,value);                
                }
        }
        
        private ConfigBean parse() throws ConfigReadException
        {
                logger.debug("parse()");
                InputStream fi = null;
                ConfigBean bean = null;
                try
                {
                        fi = this.getClass().getClassLoader().getResourceAsStream("config/" + this.entityName + ".xml");
                        SAXBuilder sb = new SAXBuilder();
                        Document doc = sb.build(fi);
                        Element root = doc.getRootElement(); //得到根元素
                        // 是否采用缓存
                        String cachable = root.getChild("cachable").getText();
                        // 驾险
                        String main = root.getChild("entity").getChild("main").getText();              
                        // 关联体名                                    
                        String[] relas = this.getValues(root.getChild("entity").getChildren("rela"));
                        String[] rlMethods = this.getValues(root.getChild("methods").getChildren("readonly"));
                        
                        String[] wrMethods = this.getValues(root.getChild("methods").getChildren("write"));
                        bean = new ConfigBean(Boolean.valueOf(cachable).booleanValue(),
                                              main,
                                              relas,
                                              rlMethods,
                                              wrMethods);
                }
                catch(Exception e)
                {
                        throw new ConfigReadException("解析" +  this.entityName + ".xml 异常" + e.toString());
                }
                finally
                {
                        try
                        {
                                fi.close();
                        }
                        catch(Exception e)
                        {
                                logger.error("关闭异常:" + e.toString());
                        }       
                }    
                return bean;            
        }
        
        private String[] getValues(List nodes) throws Exception
        {
                if(nodes == null)
                        return new String[0];
                String[] values = new String[nodes.size()];                        
                for(int i = 0; i < nodes.size(); i ++)
                {
                        Element ele = (Element)nodes.get(i);
                        values[i] = ele.getText();   
                }    
                return values;                                    
        }
}
